﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace Jy123.Web.Manage.User.Gift
{
    public partial class Index :MgConfig //System.Web.UI.Page
    {
        protected int pagesize = 10;
        protected int cpage = 1;//当前页
        protected List<int?> count;

        Jy123.Model.Public_Gift modelGift = new Jy123.Model.Public_Gift();
        Jy123.BLL.Public_Gift bllGift = new Jy123.BLL.Public_Gift();

        protected void Page_Load(object sender, EventArgs e)
        {
            Verify("index.aspx");
            if (!IsPostBack)
            {
                BindddlClass();
            }
            

            if (Request.QueryString["page"] != null)
            {
                cpage = Convert.ToInt32(Request.QueryString["page"]);
            }
            count = new List<int?>();

            string type = Request.QueryString["Type"];
            if (type == "del")
            {
                new BLL.Public_Gift().Delete(Convert.ToInt32(Request.QueryString["id"]));

            }
            else if (type == "Islock")//是否锁定
            {
                modelGift = bllGift.GetModel(Convert.ToInt32(Request.QueryString["id"]));
                if (int.Parse(Request.QueryString["flag"]) == 1)
                {
                    modelGift.IsLock = true;
                    bllGift.Update(modelGift);
                }
                else if (int.Parse(Request.QueryString["flag"]) == 2)
                {
                    modelGift.IsLock = false;
                    bllGift.Update(modelGift);
                }
            }
            BindRpt();
        }
        
        /// <summary>
        /// 绑定列表
        /// </summary>
        private void BindRpt()
        {
            RptData.DataSource = new BLL.User.Gift().ManageGiftList(cpage, pagesize, "", out count);
            RptData.DataBind(); 
        }
        /// <summary>
        /// 显示状态
        /// </summary>
        /// <param name="islock"></param>
        /// <returns></returns>
        protected string ShowLock(object islock)
        {
            if (islock != null && islock != DBNull.Value && (bool)islock == true)
            {
                return "<a class=\"cred\">锁定</a>";
            }
            else
            {
                return "正常";
            }
        }
        /// <summary>
        /// 显示锁定按钮
        /// </summary>
        /// <param name="islock"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        protected string ShowLock(object islock, object id)
        {
            if (islock != null && islock != DBNull.Value && (bool)islock == true)
            {
                return "<a href=\"javascript:UnLockIt('" + id + "');\">解锁</a>";
            }
            else
            {
                return "<a href=\"javascript:LockIt('" + id + "');\">锁定</a>";
            }
        }
        /// <summary>
        /// 锁定按钮文字及样式替换
        /// </summary>
        /// <param name="islock">是否锁定(绑定的值)</param>
        /// <param name="id">绑定的记录ID</param>
        /// <param name="page">页码</param>
        /// <returns>返回新按钮文字及样式</returns>
        protected string Lock(object islock, object id, object page)
        {
            if (islock != null && Convert.ToInt16(islock) == 0)
            {
                return "<a class=\"btnsty cred\" href=\"javascript:Islock(" + id + ",1,'" + page + "');\">锁定</a>";
            }
            else
            {
                return "<a class=\"btnsty\" href=\"javascript:Islock(" + id + ",2,'" + page + "');\">取消锁定</a>";
            }
        }
        /// <summary>
        /// 绑定礼物分类下拉列表
        /// </summary>
        private void BindddlClass()
        {
            ddlGiftClass.DataSource = new BLL.Public_GiftClass().GetAllList();
            ddlGiftClass.DataTextField = "GiftClassName";
            ddlGiftClass.DataValueField = "ID";
            ddlGiftClass.DataBind();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string giftClassName = ddlGiftClass.SelectedValue;
            RptData.DataSource = new BLL.User.Gift().ManageGiftList(cpage, pagesize, "FGiftClass ='"+giftClassName+"' and GName like '%"+txtSearch.Text+"%'", out count);
            RptData.DataBind(); 
        }

        protected void btnAll_Click(object sender, EventArgs e)
        {
            BindRpt();
        }

    }
}
